<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>淘有货后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/static/images/logo/favicon.ico"
          type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/main/xadmin.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/swiper.min.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/login/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/swiper.jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/main/xadmin.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/api/adminStore.js"></script>

    <style type="text/css">
        /* 	.page-loader {
    background:#0e1319 url(images/bg.jpg) no-repeat center;
    background-size:cover;
    position:fixed;
    top:0;
    left:0;
    right:0;
    width:100%;
    height:100%;
    z-index:999999;
}

.page-loader a {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    margin-top: -54px;
}

.page-loader span {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    margin-top: 60px;
    color: #fff;
    opacity: 0.5;
    font-size: 14px;
    letter-spacing: 0.4em;
}
/* bootstrap */
        .img-responsive {
            display: block;
            max-width: 100%;
            height: auto;
        }

        .center-block {
            display: block;
            margin-right: auto;
            margin-left: auto;
        }

        .text-uppercase {
            text-transform: uppercase;
        }
    </style>

</head>
<body>
<!-- 顶部开始 -->
<div class="container">
    <jsp:include page="../header.jsp"/>
</div>
<!-- 顶部结束 -->
<!-- 中部开始 -->
<div class="wrapper">
    <!-- 左侧菜单开始 -->
    <jsp:include page="../menu.jsp"/>
    <!-- 左侧菜单结束 -->
    <!-- 右侧主体开始 -->
    <div class="page-content">
        <div class="content">
            <!-- 右侧内容框架，更改从这里开始 -->

            <div class="layui-form-pane" style="text-align: center;">
                <div class="layui-form-item" style="display: inline-block;">
                    <!-- <label class="layui-form-label xbs768">日期范围</label>
        <div class="layui-input-inline xbs768">
          <input class="layui-input" placeholder="开始日" id="LAY_demorange_s">
        </div>
        <div class="layui-input-inline xbs768">
          <input class="layui-input" placeholder="截止日" id="LAY_demorange_e">
        </div>时间选择器 -->
                    <div class="layui-input-inline">
                        <input id="roleName" type="text" name="username" placeholder="请输入商品名" autocomplete="off"
                               class="layui-input">
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button id="btnsearch" class="layui-btn" lay-filter="sreach"><i
                                class="layui-icon">&#xe615;</i></button>
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button class="layui-btn" id="back">返回</button>
                    </div>
                </div>
            </div>
            <xblock>
                <button class="layui-btn" onclick="product_add()"><i class="layui-icon">&#xe608;</i>添加</button>
                <select style="margin-left: 15px;" class="layui-select x-right" name="category" id="category"
                        onchange="product_find()">
                    <c:forEach items="${category}" var="item">
                        <c:if test="${cid == item.cid}">
                            <option selected="selected" value="${item.cid}">${item.cname}</option>
                        </c:if>
                        <c:if test="${cid != item.cid}">
                            <option value="${item.cid}">${item.cname}</option>
                        </c:if>
                    </c:forEach>
                </select>
                <span class="x-right" style="line-height:40px">共有商品：${size} 件</span>
            </xblock>
            <table class="layui-table">
                <thead>
                <tr>
                    <th>
                        图片简介
                    </th>
                    <th>
                        商品名称
                    </th>
                    <th>
                        商品原价
                    </th>
                    <th>
                        商场价格
                    </th>
                    <th>
                        库存
                    </th>
                    <th>
                        总销量
                    </th>
                    <th>
                        操作
                    </th>
                </tr>
                </thead>
                <tbody>
                <c:if test="${list != null}">
                    <c:forEach items="${list}" var="product">
                        <tr>
                            <td>
                                <img style="width:50px;"
                                     src="/shopClient/${product.imgs}" alt="${product.name}">
                            </td>
                            <td>${product.name}</td>
                            <td>
                                <del>￥${product.marketPrice}</del>
                            </td>
                            <td style="color:red;">￥${product.shelfPrice}</td>

                            <td>
                                    ${product.stock}
                            </td>
                            <td>
                                    ${product.monthlySales}
                            </td>
                            <td class="td-manage">

                                <a title="编辑" href="javascript:;" onclick="product_edit('${product.pid}')" class="ml-5"
                                   style="text-decoration:none">
                                    <i class="layui-icon">&#xe642;</i>
                                </a>
                                <a title="删除" href="javascript:;" onclick="product_del(this,'${product.pid}')"
                                   style="text-decoration:none">
                                    <i class="layui-icon">&#xe640;</i>
                                </a>
                                <c:if test="${product.state == '0'}">
                                        <span style="width: 56px;" class="layui-btn layui-btn-danger layui-btn-small"
                                              onclick="product_state(this,'${product.pid}')">下架中</span>
                                </c:if>
                                <c:if test="${product.state == '1'}">
                                        <span style="width: 56px;" class="layui-btn layui-btn-normal layui-btn-small"
                                              onclick="product_state(this,'${product.pid}')">上架中</span>
                                </c:if>

                                <a title="库存修改" href="javascript:;" onclick="stock_edit('${product.pid}')" class="ml-5"
                                   style="text-decoration:none">
                                    <i class="layui-icon">&#xe642;</i>
                                </a>
                            </td>
                        </tr>
                    </c:forEach>
                </c:if>
                </tbody>
            </table>
            <!-- 右侧内容框架，更改从这里结束 -->
        </div>
    </div>
    <!-- 右侧主体结束 -->
</div>
<!-- 中部结束 -->
<!-- 底部开始 -->
<!-- 		<div class="footer">
			<div class="copyright">Copyright ©2017 x-admin v2.3 All Rights Reserved. 本后台系统由X前端框架提供前端技术支持</div>
		</div> -->
<!-- 底部结束 -->
<!-- 背景切换开始 -->
<jsp:include page="../imagechange.jsp"/>
<!-- 背景切换结束 -->
<!-- 页面动态效果 -->

<!-- 		//Page Loader
<div class="page-loader">
    <a href="#"><img src="images/logo-2.png" class="img-responsive center-block" alt=""/></a>
    <span class="text-uppercase">Loading...</span>
</div>
<script src="js/loader.js"></script> 页面加载 -->


<script>

    /*浏览-删除*/
    function product_del(obj, pid) {
        layer.confirm('确认要删除吗？', function (index) {


            $.ajax({
                type: "post",
                url: adminStore.context + "/product/delProduct.do",
                data: {
                    pid: pid
                },
                success: function (s) {
                    if (s.success) {
                        //发异步删除数据
                        $(obj).parents("tr").remove();
                        layer.msg(s.msg, {
                            icon: 1,
                            time: 1000
                        });
                        <%--top.location.href = encodeURI("${pageContext.request.contextPath}/main.do?admin=****&url=product");--%>
                    } else {
                        alert(s.msg);
                    }
                }
            })
        });
    }

    product_state = function (data, pid) {
        if ($(data).hasClass("layui-btn-danger")) {
            layer.confirm("是否上架商品？", function () {
                update_state(data, pid);
            })
        }
        if ($(data).hasClass("layui-btn-normal")) {
            layer.confirm("商品售卖中，确认要下架吗？", function () {
                update_state(data, pid);
            })
        }
    }

    update_state = function (data, pid) {
        $.ajax({
            url: adminStore.context + "/product/updateState.do",
            data: {
                pid: pid
            },
            type: "post",
            success: function (d) {
                if (d == "-1") {
                    top.location.href = adminStore.context + "/index.do";
                } else {
                    if (d == '0') {
                        $(data).removeClass("layui-btn-danger");
                        $(data).addClass("layui-btn-normal");
                        $(data).html("上架中");
                        layer.msg("上架商品成功", {
                            icon: 1,
                            time: 1000
                        })
                    }
                    if (d == '1') {
                        $(data).addClass("layui-btn-danger");
                        $(data).removeClass("layui-btn-normal");
                        $(data).html("下架中");
                        layer.msg("已下架商品", {
                            icon: 1,
                            time: 1000
                        })
                    }
                }
            }
        })
    }


    $("#btnsearch").click(function () {
        var roleName = $("#roleName").val();
        if (roleName.length == 0 || roleName.indexOf(" ") != -1) {
            top.location.reload();
        }
        window.location.href = adminStore.context + "/product/findProduct.do?name=" + roleName;
    })

    $("#back").click(function () {
        location.href = adminStore.context + "/product/product.do";
    })
</script>
<script>
    //百度统计可去掉
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();

    // 自己写的
    product_add = function () {
        var url = adminStore.context + "/product/toAddProduct.do";
        x_admin_show(
            "商品添加",
            url,
            "460", "500")
    }

    product_edit = function (id) {
        var url = adminStore.context + "/product/toEditProduct.do?pid=" + id;
        x_admin_show(
            "商品修改",
            url,
            "460", "500")
    }

    stock_edit = function (pid) {
        var url = adminStore.context + "/product/toEditStock.do?pid=" + pid;
        x_admin_show(
            "库存修改",
            url,
            "460", "180")
    }

    product_find = function () {
        var cid = $("#category").val();
        top.location.href = adminStore.context + "/product/changeCategory.do?cid=" + cid;
    }
</script>
</body>
</html>
